package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseConfirmDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CertificateNoContent;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.FileNetWorkUtil;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class AddTravelReimburseDetailFragment extends TravelReimburselFragment {
  public static final String EXTRA_CODE = "com.isunland.managesystem.ui.EXTRA_CODE";
  protected static final String TAG = AddTravelReimburseDetailFragment.class.getSimpleName();
  private String code;
  private CertificateNoContent mNoContent;
  private CurrentUser mUser;
  private StringBuilder mManagecode = new StringBuilder();
  private Double replenishAmount = 0.0;//本次预存票据

  @Override public void onCreate(Bundle savedInstanceState) {
    code = getArguments().getString(EXTRA_CODE);
    mUser = CurrentUser.newInstance(getActivity());
    //自动生成id
    id = UUID.randomUUID().toString();
    super.onCreate(savedInstanceState);
  }

  @Override protected void setTitle() {
    mManagecode.append(mUser.getMemberCode()).append("-").append("DJLB-CWZC-GLFYBX");
    if ((mManagecode.toString()).equals(code)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.manageReimburse);
    } else {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.travelReimburse);
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    //		inflater.inflate(R.menu.menu_confirm, menu);
    inflater.inflate(R.menu.menu_save_submit, menu);
    menu.getItem(1).setTitle(R.string.saveAndSubmit);
  }

  @Override protected void initData() {
    // 获取单据编号

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_ADD_EMPLOYEE_LOAN_DETAIL);

    HashMap<String, String> params = new HashMap<>();

    params.put("certificateCode", code);
    params.put("userJobNo", mCurrentUser.getJobNumber());
    params.put("year", MyDateUtil.format(new Date(),MyDateUtil.TYPE_YEAR));
    LogUtil.i("params==" + params);
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        CertificateNoContent[] array = new Gson().fromJson(arg0, CertificateNoContent[].class);
        mNoContent = array[0];

        tvcertificateNo.setText(mNoContent.getCertificateNo());
        tvsumBorrow.setText(mNoContent.getSumBorrow());

        //				 tvsumPrebill.setText(mNoContent.getSumPrebill());//合计预存票据
        etpayeeBank.setText(mNoContent.getPayeeBank());
        etpayeeAccname.setText(mNoContent.getPayeeAccname());
        etpayeeAccount.setText(mNoContent.getPayeeAccount());

        tvfownerkindName.setText(fownerkindName);//归属分类
        tvappobjectName.setText(appobjectName);//归属对象

        tvregStaffName.setText(mCurrentUser.getRealName());
        tvapplicantName.setText(mCurrentUser.getRealName());
        tvapplyTime.setText(MyDateUtil.format(new Date(),MyDateUtil.TYPE_ALL));
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  // 菜单点击事件
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //			if (NavUtils.getParentActivityName(getActivity()) != null) {
        //				NavUtils.navigateUpFromSameTask(getActivity());
        //			}
        getActivity().finish();
        return true;

      // 保存
      case R.id.menu_item_save:

        if (TextUtils.isEmpty(tvcertificateNo.getText().toString())) {
          Toast.makeText(getActivity(), R.string.noCertificateNo, Toast.LENGTH_SHORT).show();
          return true;
        }
        if (TextUtils.isEmpty(etoffBorrow.getText().toString()) || TextUtils.isEmpty(
            etpoutAmount.getText().toString()) || TextUtils.isEmpty(
            ettoutAmount.getText().toString())) {
          Toast.makeText(getActivity(), R.string.noTure, Toast.LENGTH_SHORT).show();
          return true;
        }
        if (TextUtils.isEmpty(tvappobjectName.getText().toString())) {
          Toast.makeText(getActivity(), R.string.noAppobjectName, Toast.LENGTH_SHORT).show();
          return true;
        }
        /**
         * 应报销    抵扣  实际报销  借款  有票  无票  预存票

         1. 抵扣的值<= 历史借款

         2. 无票的值 = 0

         3. 实际报销+抵扣的和 = 应报销

         4. 有票= 应报销

         5.本次预存票据费用=0；
         */
        //			Double sumPrebill=Double.parseDouble( tvsumPrebill.getText().toString());
        //合计>=冲抵
        Double sumBorrow = Double.parseDouble(tvsumBorrow.getText().toString());
        Double offBorrow = Double.parseDouble(etoffBorrow.getText().toString());
        double result = sumBorrow - offBorrow;

        LogUtil.i(offBorrow + "");
        //应支出金额 = 实际支出+抵扣借款
        Double toutAmount = Double.parseDouble(etpoutAmount.getText().toString());//应报销
        Double poutAmount = Double.parseDouble(ettoutAmount.getText().toString());//实际报销
        double result1;
        //如果选择冲抵  应支出金额 = 实际支出+抵扣借款
        if (TRUE.equals(tag)) {
          result1 = poutAmount + offBorrow;
        }
        //不选冲抵  应支出金额 = 实际支出
        else {
          result1 = poutAmount;
        }

        //判断填写的数据关系
        if (result < 0) {
          Toast.makeText(getActivity(), R.string.nobig, Toast.LENGTH_SHORT).show();
        } else if (result >= 0 && toutAmount >= result1) {
          /**
           * 修改添加附件
           */
          //判断有无附件
          if (TextUtils.isEmpty(mFilePath)) {
            //附件为空时
            volleyPostSave(SAVE);
          } else {
            //附件不为空时，先上传附件，再保存数据
            //									//自动生成id
            //							    id=UUID.randomUUID().toString();
            MyUtils.showDialogLoading(getActivity());
            volleyExtra(SAVE);
          }
        } else {
          Toast.makeText(getActivity(), R.string.moneyFalse, Toast.LENGTH_SHORT).show();
        }

        //			if (TextUtils.isEmpty(tvcertificateNo.getText().toString())) {
        //				Toast.makeText(getActivity(),R.string.noCertificateNo, Toast.LENGTH_SHORT).show();
        //				return true;
        //			}
        //			if (TextUtils.isEmpty(etoffBorrow.getText().toString()) || TextUtils.isEmpty(etpoutAmount.getText().toString())
        //					|| TextUtils.isEmpty(ettoutAmount.getText().toString())) {
        //				Toast.makeText(getActivity(),R.string.noTure, Toast.LENGTH_SHORT).show();
        //				return true;
        //			}
        //			/**
        //			 * 应报销    抵扣  实际报销  借款  有票  无票  预存票
        //
        //				1. 抵扣的值<= 历史借款
        //
        //				2. 无票的值 <= 预存票
        //
        //				3. 实际报销+抵扣的和 <= 应报销
        //
        //				4. 有票+无票的和  >= 应报销
        //
        //				5.本次预存票据费用=票据费用+无票费用-应报销费用。小于0就默认为0；
        //			 */
        //			Double sumPrebill=Double.parseDouble( tvsumPrebill.getText().toString());
        //			//合计>=冲抵
        //			Double sumBorrow=Double.parseDouble( tvsumBorrow.getText().toString());
        //			Double offBorrow=Double.parseDouble(etoffBorrow.getText().toString());
        //			double result = sumBorrow- offBorrow;
        //
        //			LogUtil.i(offBorrow+"");
        //			//应支出金额 >= 实际支出+抵扣借款
        //			Double toutAmount=Double.parseDouble( etpoutAmount.getText().toString());//应报销
        //			Double poutAmount=Double.parseDouble( ettoutAmount.getText().toString());//实际报销
        //			double result1=0;
        //			//如果选择冲抵  应支出金额 >= 实际支出+抵扣借款
        //			if (TRUE_FINISH_RECEIVE_ORDER.equals(tag)) {
        //				result1 = poutAmount+offBorrow;
        //				LogUtil.i("result1-----"+"result1 = poutAmount+offBorrow;");
        //			}
        //			//不选冲抵  应支出金额 >= 实际支出
        //			else {
        //				result1 = poutAmount;
        //				LogUtil.i("result1-----"+"result1 = poutAmount");
        //			}
        //
        //			//判断填写的数据关系
        //			if (result<0) {
        //				Toast.makeText(getActivity(),R.string.nobig, Toast.LENGTH_SHORT).show();
        //			}else if (result>=0 && toutAmount >= result1) {
        //
        //				if (!TextUtils.isEmpty(etnobillAmount.getText().toString()) ) {
        //					//2. 无票的值 <= 预存票
        //					Double nobillAmount=Double.parseDouble( etnobillAmount.getText().toString());
        //
        //					if (nobillAmount>sumPrebill) {
        //						Toast.makeText(getActivity(),"无票费用不能大于合计预存票据", Toast.LENGTH_SHORT).show();
        //					}else {
        //						if (!TextUtils.isEmpty(etbillAmount.getText().toString())) {
        //							Double billAmount=Double.parseDouble( etbillAmount.getText().toString());
        //							if (nobillAmount+billAmount < toutAmount) {
        //								Toast.makeText(getActivity(),"无票与有票的和不能小于应报销", Toast.LENGTH_SHORT).show();
        //							}else {
        //
        ////								volleyPostSave();
        //								/**
        //								 * 修改添加附件
        //								 */
        //							   //判断有无附件
        //								if (TextUtils.isEmpty(mDocPath)) {
        //									//附件为空时
        //									volleyPostSave();
        //
        //									//本次预存票据费用=票据费用+无票费用-应报销费用
        //									replenishAmount=nobillAmount+billAmount-toutAmount;
        //									if (replenishAmount<0) {
        //										replenishAmount=0.00;
        //									}else {
        //										replenishAmount=nobillAmount+billAmount-toutAmount;
        //									}
        //								}else{
        //									//附件不为空时，先上传附件，再保存数据
        //									//自动生成id
        //							    id=UUID.randomUUID().toString();
        //								MyUtils.showDialogLoading(getActivity());
        //								volleyExtra();
        //								}
        //
        //					       }
        //						}else {
        //							//有票费用为空时
        //							if (nobillAmount>=toutAmount) {
        ////								volleyPostSave();
        //
        //								/**
        //								 * 修改添加附件
        //								 */
        //							   //判断有无附件
        //								if (TextUtils.isEmpty(mDocPath)) {
        //									//附件为空时
        //									volleyPostSave();
        //									//本次预存票据费用=票据费用+无票费用-应报销费用
        //									replenishAmount=nobillAmount-toutAmount;
        //									if (replenishAmount<0) {
        //										replenishAmount=0.00;
        //									}else {
        //										replenishAmount=nobillAmount-toutAmount;
        //									}
        //								}else{
        //									//附件不为空时，先上传附件，再保存数据
        //									//自动生成id
        //							    id=UUID.randomUUID().toString();
        //								MyUtils.showDialogLoading(getActivity());
        //								volleyExtra();
        //
        //								}
        //							}else {
        //								Toast.makeText(getActivity(),R.string.falseMessage, Toast.LENGTH_SHORT).show();
        //							}
        //						}
        //
        //					}
        //				}else {
        //					if (!TextUtils.isEmpty(etbillAmount.getText().toString())) {
        //						Double billAmount=Double.parseDouble( etbillAmount.getText().toString());
        //						if (billAmount < toutAmount) {
        //							Toast.makeText(getActivity(),R.string.falseMessage, Toast.LENGTH_SHORT).show();
        //						}else {
        ////						volleyPostSave();
        //
        //							/**
        //							 * 修改添加附件
        //							 */
        //						   //判断有无附件
        //							if (TextUtils.isEmpty(mDocPath)) {
        //								//附件为空时
        //								volleyPostSave();
        //								//本次预存票据费用=票据费用+无票费用-应报销费用
        //								replenishAmount=billAmount-toutAmount;
        //								if (replenishAmount<0) {
        //									replenishAmount=0.00;
        //								}else {
        //									replenishAmount=billAmount-toutAmount;
        //								}
        //							}else{
        //								//附件不为空时，先上传附件，再保存数据
        //								//自动生成id
        //						    id=UUID.randomUUID().toString();
        //							MyUtils.showDialogLoading(getActivity());
        //							volleyExtra();
        //
        //							}
        //				        }
        //
        //			         }else {
        //						//有票和无票都为空时
        //			        	 Toast.makeText(getActivity(),R.string.falseMessage, Toast.LENGTH_SHORT).show();
        //					}
        //
        //				}
        //			}else {
        //				Toast.makeText(getActivity(),R.string.falseMessage, Toast.LENGTH_SHORT).show();
        //			}

        return true;

      //保存并启动流程
      case R.id.menu_item_submit:

        if (TextUtils.isEmpty(tvcertificateNo.getText().toString())) {
          Toast.makeText(getActivity(), R.string.noCertificateNo, Toast.LENGTH_SHORT).show();
          return true;
        }
        if (TextUtils.isEmpty(etoffBorrow.getText().toString()) || TextUtils.isEmpty(
            etpoutAmount.getText().toString()) || TextUtils.isEmpty(
            ettoutAmount.getText().toString())) {
          Toast.makeText(getActivity(), R.string.noTure, Toast.LENGTH_SHORT).show();
          return true;
        }
        /**
         * 应报销    抵扣  实际报销  借款  有票  无票  预存票

         1. 抵扣的值<= 历史借款

         2. 无票的值 = 0

         3. 实际报销+抵扣的和 = 应报销

         4. 有票= 应报销

         5.本次预存票据费用=0；
         */
        //			Double sumPrebill=Double.parseDouble( tvsumPrebill.getText().toString());
        //合计>=冲抵
        Double sumBorrow1 = Double.parseDouble(tvsumBorrow.getText().toString());
        Double offBorrow1 = Double.parseDouble(etoffBorrow.getText().toString());
        double result2 = sumBorrow1 - offBorrow1;

        LogUtil.i(offBorrow1 + "");
        //应支出金额 = 实际支出+抵扣借款
        Double toutAmount1 = Double.parseDouble(etpoutAmount.getText().toString());//应报销
        Double poutAmount1 = Double.parseDouble(ettoutAmount.getText().toString());//实际报销
        double result3;
        //如果选择冲抵  应支出金额 = 实际支出+抵扣借款
        if (TRUE.equals(tag)) {
          result3 = poutAmount1 + offBorrow1;
          LogUtil.i("result1-----" + "result1 = poutAmount+offBorrow;");
        }
        //不选冲抵  应支出金额 = 实际支出
        else {
          result3 = poutAmount1;
          LogUtil.i("result1-----" + "result1 = poutAmount");
        }

        //判断填写的数据关系
        if (result2 < 0) {
          Toast.makeText(getActivity(), R.string.nobig, Toast.LENGTH_SHORT).show();
        } else if (result2 >= 0 && toutAmount1 >= result3) {
          /**
           * 修改添加附件
           */
          //判断有无附件
          if (TextUtils.isEmpty(mFilePath)) {
            //附件为空时
            //                        volleyPostSave(SAVE_START);
            showDialog(SAVE_START);
          } else {
            //附件不为空时，先上传附件，再保存数据
            //                        //自动生成id
            //                        id=UUID.randomUUID().toString();
            MyUtils.showDialogLoading(getActivity());
            volleyExtra(SAVE_START);
          }
        } else {
          Toast.makeText(getActivity(), R.string.moneyFalse, Toast.LENGTH_SHORT).show();
        }

        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void volleyExtra(int requestCode) {

    new FileNetWorkUtil(getActivity()).upLoadFile(mFilePath, ApiConst.URL_UPLOAD_FILE,
        "r_expense_inexp_main", id, new myUpLoadHandler(requestCode), false);
  }

  private class myUpLoadHandler extends AsyncHttpResponseHandler {
    private int requestCode;

    public myUpLoadHandler(int requestCode) {
      this.requestCode = requestCode;
    }

    @Override public void onStart() {
      MyUtils.showDialogLoading(getActivity());
      super.onStart();
    }

    @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
      //		MyUtils.dismissDialog();
      Toast.makeText(getActivity(), R.string.fileUploadFailure, Toast.LENGTH_SHORT).show();
    }

    @Override public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
      MyUtils.dismissDialog();
      LogUtil.i("arg2==" + new String(arg2));
      try {

        SuccessMessage result = new Gson().fromJson(new String(arg2), SuccessMessage.class);
        if (result == null) {
          MyUtils.dismissDialog();
          Toast.makeText(getActivity(), R.string.wrong_data, Toast.LENGTH_SHORT).show();
        }
        if (result != null && result.getResult() != null && result.getResult().equals("1")) {
          //					Toast.makeText(getActivity(), R.string.fileUploadSuccess, Toast.LENGTH_SHORT).show();
          // 上传成功,再上传数据
          selcurFile = result.getMessage();
          selcurFile = selcurFile.replace("\\", "/");
          LogUtil.i("selcurFile==" + selcurFile);
          volleyPostSave(requestCode);
        } else {
          MyUtils.dismissDialog();
          Toast.makeText(getActivity(), R.string.fileUploadFailure, Toast.LENGTH_SHORT).show();
        }
      } catch (JsonSyntaxException | NotFoundException e) {
        e.printStackTrace();
        Toast.makeText(getActivity(), R.string.fileUploadFailure, Toast.LENGTH_SHORT).show();
      }
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog =
        BaseConfirmDialogFragment.newInstance(R.string.cannotEdit, R.string.hintMessage);
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  private void volleyPostSave(final int requestCode) {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_PESONAL_SAVE);

    HashMap<String, String> params = new HashMap<>();
    HashMap<String, String> paramsItem = new HashMap<>();
    paramsItem.put("id", id);//保存时 id本地自动生成

    paramsItem.put("certificateNo", mNoContent.getCertificateNo());
    paramsItem.put("expenseDesc", etexpenseDesc.getText().toString());
    paramsItem.put("toutAmount", etpoutAmount.getText().toString());//应报销
    paramsItem.put("sumBorrow", tvsumBorrow.getText().toString());
    paramsItem.put("offBorrow", etoffBorrow.getText().toString());
    paramsItem.put("poutAmount", ettoutAmount.getText().toString());//实际报销

    paramsItem.put("receiverName", tvregStaffName.getText().toString());//报销人
    paramsItem.put("receiverId", mCurrentUser.getJobNumber());

    paramsItem.put("applicantName", tvapplicantName.getText().toString());//申请人
    paramsItem.put("applicantId", mCurrentUser.getJobNumber());

    //登记人
    params.put("regStaffName", tvapplicantName.getText().toString());
    params.put("regStaffId", mCurrentUser.getJobNumber());

    paramsItem.put("applyTime", tvapplyTime.getText().toString());
    paramsItem.put("ifOffBorrow", tag);
    paramsItem.put("dataStatus", "new");
    paramsItem.put("ifReturnLoan", "F");

    //票据

    paramsItem.put("inAmount", "0");
    paramsItem.put("sumPrebill", mNoContent.getSumPrebill());//合计预存票
    paramsItem.put("replenishAmount", "0");//本次预存票据
    paramsItem.put("billAmount", etbillAmount.getText().toString());//票据费用
    paramsItem.put("billNo", "");//票据编码
    paramsItem.put("nobillAmount", "0");//无票费用
    //		paramsItem.put("deductionAmount", etdeductionAmount.getText().toString());//冲抵预存票
    paramsItem.put("deductionAmount", "0");//冲抵预存票

    //		paramsItem.put("inAmount", "0");
    //		paramsItem.put("sumPrebill", mNoContent.getSumPrebill());
    //		paramsItem.put("replenishAmount", replenishAmount+"");//本次预存票据

    ////		if (!TextUtils.isEmpty(etbillNo.getText().toString())) {
    ////			paramsItem.put("billNo", etbillNo.getText().toString());
    ////		}else {
    ////			paramsItem.put("billNo", "");
    ////		}
    //		paramsItem.put("billNo", "");
    //		if (!TextUtils.isEmpty(etnobillAmount.getText().toString())) {
    //			paramsItem.put("nobillAmount", etnobillAmount.getText().toString());
    //		}else {
    //			paramsItem.put("nobillAmount", "0");
    //		}

    //开户行
    if (!TextUtils.isEmpty(etpayeeBank.getText().toString())) {
      paramsItem.put("payeeBank", etpayeeBank.getText().toString());
    } else {
      paramsItem.put("payeeBank", "");
    }
    if (!TextUtils.isEmpty(etpayeeAccname.getText().toString())) {
      paramsItem.put("payeeAccname", etpayeeAccname.getText().toString());
    } else {
      paramsItem.put("payeeAccname", "");
    }
    if (!TextUtils.isEmpty(etpayeeBank.getText().toString())) {
      paramsItem.put("payeeAccount", etpayeeAccount.getText().toString());
    } else {
      paramsItem.put("payeeAccount", "");
    }
    //费用归属
    paramsItem.put("fownerkindName", fownerkindName);//分类
    paramsItem.put("fownerkindCode", fownerkindCode);//分类code，默认是公司费用
    paramsItem.put("appobjectName", appobjectName);//对象
    paramsItem.put("appobjectId", appobjectCode);//对象id,公司会员编码

    if (!TextUtils.isEmpty(tvcontractId.getText().toString())) {

      paramsItem.put("contractId", mContractId);
      paramsItem.put("contractName", mContractName);
      paramsItem.put("contractKind", mContractKind);
      paramsItem.put("contractCode", mContractCode);

      paramsItem.put("orderId", mOrderId);
      paramsItem.put("orderName", mOrderName);
      paramsItem.put("orderType", mOrderType);

      paramsItem.put("contractProjectId", mProjectId);
      paramsItem.put("contractProjectName", mProjectName);

      if (mContractPrice == null || TextUtils.isEmpty(mContractPrice) || "null".equals(
          mContractPrice)) {
        paramsItem.put("contractAmount", "0");
      } else {
        paramsItem.put("contractAmount", mContractPrice);
      }
    }

    //新增附件上传
    paramsItem.put("filePath", selcurFile);
    paramsItem.put("fileOriginalName", mfileName);
    if (!TextUtils.isEmpty(selcurFile)) {
      paramsItem.put("id", id);
    }

    String test = new JSONObject(paramsItem).toString();

    params.put("json", test);
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("certificateCode", code);
    params.put("regJobNo", mCurrentUser.getJobNumber());
    params.put("type", "mobile");
    LogUtil.i("添加合同参数===" + params.toString());
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        MyUtils.dismissDialog();
        SuccessMessage[] result = new Gson().fromJson(arg0, SuccessMessage[].class);
        SuccessMessage workProcess = result[0];
        if (null == workProcess || null == workProcess.getResult()) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        String successType = workProcess.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          if (SAVE == requestCode) {
            Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
          } else if (SAVE_START == requestCode) {
            //启动流程
            volleyStart();
          }
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  // 启动流程方法
  private void volleyStart() {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_START_UP);

    HashMap<String, String> params = new HashMap<>();
    params.put("isList", "1");
    params.put("id", id);
    /**
     * 添加界面获取不到flowFlag
     * */
    //        params.put("recFlowKey", mEmployeeLoanContent.getFlowFlag());
    params.put("certificateCode", code);
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        MyUtils.dismissDialog();
        SuccessMessage workProcess = new Gson().fromJson(arg0, SuccessMessage.class);
        if (null == workProcess || null == workProcess.getResult()) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();

          //重新启动时的错误log日志
          LogUtil.e(workProcess.getMessage());

          return;
        }
        String successType = workProcess.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          MyUtils.dismissDialog();
          Toast.makeText(getActivity(), R.string.failstartup, Toast.LENGTH_SHORT).show();
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();
          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == SAVE_START) {
      volleyPostSave(SAVE_START);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  // fragment之间传值
  public static AddTravelReimburseDetailFragment newInstance(String code) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CODE, code);
    AddTravelReimburseDetailFragment fragment = new AddTravelReimburseDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
